package com.gxp.system.service.impl;

import com.gxp.common.core.text.Convert;
import com.gxp.common.utils.DateUtils;
import com.gxp.common.utils.StringUtils;
import com.gxp.system.domain.ZknjMethod;
import com.gxp.system.domain.ZknjService;
import com.gxp.system.mapper.ZknjMethodMapper;
import com.gxp.system.mapper.ZknjServiceMapper;
import com.gxp.system.service.IZknjMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 方法Service业务层处理
 * 
 * @author zh
 * @date 2024-07-19
 */
@Service
public class ZknjMethodServiceImpl implements IZknjMethodService 
{
    @Autowired
    private ZknjMethodMapper zknjMethodMapper;
    @Autowired
    private ZknjServiceMapper serviceMapper;

    /**
     * 查询方法
     * 
     * @param modId 方法主键
     * @return 方法
     */
    @Override
    public ZknjMethod selectZknjMethodByModId(Long modId)
    {
        return zknjMethodMapper.selectZknjMethodByModId(modId);
    }

    /**
     * 查询方法列表
     * 
     * @param zknjMethod 方法
     * @return 方法
     */
    @Override
    public List<ZknjMethod> selectZknjMethodList(ZknjMethod zknjMethod)
    {
        return zknjMethodMapper.selectZknjMethodList(zknjMethod);
    }

    /**
     * 新增方法
     * 
     * @param zknjMethod 方法
     * @return 结果
     */
    @Override
    public int insertZknjMethod(ZknjMethod zknjMethod)
    {
        zknjMethod.setModStatus(2l);
        zknjMethod.setIsLock(0l);
        zknjMethod.setApproveType("Add Method");
        zknjMethod.setCreateTime(DateUtils.getNowDate());
        return zknjMethodMapper.insertZknjMethod(zknjMethod);
    }

//    /**
//     * 新增方法
//     *
//     * @param zknjMethod 方法
//     * @return 结果
//     */
//    @Override
//    public int insertZknjMethod(ZknjMethod zknjMethod, String solvIds) {
//        insertZknjMethod(zknjMethod);
//        Long modId = zknjMethod.getModId();
//        if (solvIds.indexOf(",") > 0) {
//            for (String s : solvIds.split(",")) {
//                Long solventId = Long.valueOf(s);
//                ZknjModSolv modSolv = new ZknjModSolv();
//                modSolv.setModId(modId);
//                modSolv.setSolventId(solventId);
//                modSolvMapper.insertZknjModSolv(modSolv);
//            }
//        }else {
//            Long solventId = Long.valueOf(solvIds);
//            ZknjModSolv modSolv = new ZknjModSolv();
//            modSolv.setModId(modId);
//            modSolv.setSolventId(solventId);
//            modSolvMapper.insertZknjModSolv(modSolv);
//        }
//        return 1;
//    }

    /**
     * 修改方法
     * 
     * @param zknjMethod 方法
     * @return 结果
     */
    @Override
    public int updateZknjMethod(ZknjMethod zknjMethod)
    {
        zknjMethod.setUpdateTime(DateUtils.getNowDate());
        zknjMethod.setIsLock(zknjMethod.getModStatus());
        zknjMethod.setModStatus(2l);
        zknjMethod.setApproveType("Update Method");
        updateServiceStatus(zknjMethod);
        return zknjMethodMapper.updateZknjMethod(zknjMethod);
    }

    /**
     * 批量删除方法
     * 
     * @param modIds 需要删除的方法主键
     * @return 结果
     */
    @Override
    public int deleteZknjMethodByModIds(String modIds)
    {
        String[] strArray = Convert.toStrArray(modIds);
        for (String s : strArray) {
            Long modId = Long.valueOf(s);
            ZknjMethod zknjMethod = new ZknjMethod();
            zknjMethod.setModId(modId);
            zknjMethod.setModStatus(2l);
            zknjMethod.setApproveType("Delete Method");
            zknjMethodMapper.updateZknjMethod(zknjMethod);
        }
        return 1;
    }

    /**
     * 删除方法信息
     * 
     * @param modId 方法主键
     * @return 结果
     */
    @Override
    public int deleteZknjMethodByModId(Long modId)
    {
        return zknjMethodMapper.deleteZknjMethodByModId(modId);
    }

    /**
     * 激活方法
     * @param zknjMethod
     * @return
     */
//    @Override
//    public int active(ZknjMethod zknjMethod) {
//        //查找已激活的方法
//        zknjMethod.setModActived("Y");
//        List<ZknjMethod> zknjMethods = zknjMethodMapper.selectZknjMethodList(zknjMethod);
//        if(zknjMethods.size() == 1 && zknjMethods.get(0).getModId() == zknjMethod.getModId()){
//            //已被激活
//            return 1;
//        }else {
//            //失活
//            zknjMethods.forEach(method -> {
//                ZknjMethod m = new ZknjMethod();
//                m.setModId(method.getModId());
//                m.setModActived("N");
//                zknjMethodMapper.updateZknjMethod(m);
//            });
//            //激活
//            return zknjMethodMapper.updateZknjMethod(zknjMethod);
//        }
//        return zknjMethodMapper.updateZknjMethod(zknjMethod);
//    }

    /**
     * 方法审核
     * @param zknjMethod
     * @return
     */
    @Override
    public int approve(ZknjMethod zknjMethod) {
        if(zknjMethod.getModStatus() == 3){
            //审核通过
            ZknjMethod methodOld = zknjMethodMapper.selectZknjMethodByModId(zknjMethod.getModId());
            zknjMethod.setModStatus(methodOld.getIsLock());

            if(methodOld.getApproveType()!=null && methodOld.getApproveType().equals("Delete Method")){
                zknjMethodMapper.deleteZknjMethodByModId(zknjMethod.getModId());
            }
        }
        //修改Service 状态
        int i = zknjMethodMapper.updateZknjMethod(zknjMethod);
        updateServiceStatus(zknjMethod);
        return i;
    }

    /**
     * 同步修改服务状态
     * @param zknjMethod
     */
    private void updateServiceStatus(ZknjMethod zknjMethod) {
        ZknjMethod method = zknjMethodMapper.selectZknjMethodByModId(zknjMethod.getModId());
        ZknjService zknjService = serviceMapper.selectZknjServiceBySerId(method.getSerId());
        Long serStatus = zknjService.getSerStatus();
        List<ZknjMethod> methods = zknjMethodMapper.selectZknjMethodBySerId(method.getSerId());
        int size = methods.stream().filter(m -> m.getModStatus() == 0l).collect(Collectors.toList()).size();
        if(size > 0 && serStatus == 6){
            //Penging method  ->  Active
            ZknjService zknjServiceNew = new ZknjService();
            zknjServiceNew.setSerId(method.getSerId());
            zknjServiceNew.setSerStatus(0l);
            serviceMapper.updateZknjService(zknjServiceNew);
        }
        if(size == 0 && serStatus == 0){
            //Active  ->  Penging method
            ZknjService zknjServiceNew = new ZknjService();
            zknjServiceNew.setSerId(method.getSerId());
            zknjServiceNew.setSerStatus(6l);
            serviceMapper.updateZknjService(zknjServiceNew);
        }
    }

    /**
     * 根据条件查询列表（增加solvent映射）
     * @param zknjMethod
     * @return
     */
    @Override
    public List<ZknjMethod> selectZknjMethods(ZknjMethod zknjMethod) {
        List<ZknjMethod> methods = zknjMethodMapper.selectZknjMethods(zknjMethod);
        return methods;
    }

    @Override
    public boolean checkModNameUnique(ZknjMethod zknjMethod) {
        System.out.println("zknjMethod = " + zknjMethod);
        Long modId = zknjMethod.getModId() == null ? -1L : zknjMethod.getModId();
        ZknjMethod method = zknjMethodMapper.checkModNameUnique(zknjMethod.getModName(), zknjMethod.getSerId());
        if(StringUtils.isNotNull(method) && method.getModId().longValue() != modId.longValue()){
            return false;
        }
        return true;
    }
}
